﻿using KTD.Web.Models;
using KTD.Web.Models.VD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XXS.Common.BaseDataAccessLayer;

namespace KTD.Web.BLL
{
    public class AttachmentManager
    {
        private readonly IBaseDAL<Attachment> _AttachmentDal;
        //public GeneralViewManager() : this(DbContextManager.GetCurrentDal<DB>())
        //{
        //}
        public AttachmentManager()
        {
            _AttachmentDal = new BaseDAL<Attachment>(new DB());
        }

        public virtual IEnumerable<AttachmentVD> GetListBy(AttachmentQD im)
        {
            var res = _AttachmentDal.FindList();
            if (im.OSSId != null)
            {
                res = res.Where(n => n.OSSId == im.OSSId);
            }


            var result = res.ToList();
            var list = result.OrderByDescending(n => n.CreateDateTime).Select(n => new AttachmentVD().MapFrom(n));
            return list;
        }


        public void Create(AttachmentCUD im)
        {
            Attachment model = new Attachment();
            //model.Name = im.File.FileName;
            //model.Length = im.File.ContentLength;
            //model.ContentType = im.File.ContentType;
            //model.Ext = "." + im.File.ContentType;
            im.MapTo(model);
            _AttachmentDal.Create(model);
        }

        public AttachmentVD Find(Guid id)
        {
            Attachment model = _AttachmentDal.Find(id);

            return new AttachmentVD().MapFrom(model);
        }

        public void Delete(Guid id)
        {
            Attachment model = _AttachmentDal.Find(id);

            if (model == null)
            {
                throw new Exception("错误的ID");
            }
            _AttachmentDal.Delete(model);
        }
    }
}